local base = game_mode.localbase
local mode = class("random_mode",base)
mode.mapBuff = {}
mode.itemGenCD = 30
mode.itemGenTypes = {1,2,3,4,5,6,7}

mode.npcGenCD = 5
mode.npcGenTypes = {1,2,3,4,5,6,7,8}
mode.npcGenCollCheck = false


mode.enablePlayerRebirth = true
mode.playerRebirthCD = 1
mode.playerBirthCollCheck = false
mode.playerGenTypes = {1,2,3,4,5,6,7,8}

function mode:init()
    base.init(self)
    self.defaultAI = love.filesystem.read("res/tank.ai")
    self.map:random(40,40)
    self:initItemGen()
    self:initNPCGen()
    self:setPlayerBirthPos()
    self:setPlayerTank()
end


function mode:update(dt)
    base.update(self,dt)
    self:updateItemGen(dt)
    self:updateNPCGen(dt)
end


function mode:setMap()
    self.map:random(40,40)
end

function mode:initItemGen(timeCD,types)
    if self.itemGenCD <= 0 then return end
    self.enableItemGen = true
   	self.itemGenTimer = self.itemGenCD
end

function mode:updateItemGen(dt)
    if not self.enableItemGen then return end
    self.itemGenTimer = self.itemGenTimer - dt
	if self.itemGenTimer<0 then
		self.itemGenTimer = self.itemGenCD
		local item = Item(
            self,
			math.random(1,self.map.w*16),
			math.random(1,self.map.h*16),
			2,
			self.itemGenTypes[math.random(1,#self.itemGenTypes)]
		)
		self:setLocalObj(item)
	end
end

function mode:initNPCGen()
    if self.npcGenCD <= 0 then return end
    self.enableNPCGen = true
    self.npcGenTimer = self.npcGenCD
    self:setNPCGenPos()
end

function mode:setNPCGenPos()
    self.npcGenPos = {
        minX = 1,
        maxX = self.map.w-1,
        minY = 1,
        maxY = 1}
end


function mode:updateNPCGen(dt)
    if not self.enableNPCGen then return end
    self.npcGenTimer = self.npcGenTimer - dt
	if self.npcGenTimer<0 then
		self.npcGenTimer = self.npcGenCD
		local tank = self:newTank(
            2,
            self.npcGenTypes[math.random(1,#self.npcGenTypes)],
            self.npcGenPos,
            self.npcGenCollCheck)
		tank:setAI(self.defaultAI)
		self:setLocalObj(tank)
	end
end


function mode:setPlayerBirthPos()
    self.playerGenPos = {
        minX = 1,
        maxX = self.map.w-1,
        minY = 1,
        maxY = self.map.h-1
        }
end

function mode:setPlayerTank()
    local tank = self:newTank(
        1,
        self.playerGenTypes,
        self.playerGenPos,
        self.playerBirthCollCheck)
    self:setPlayer(tank,self:newLocalID())
end
local colltag = {
    brick = true,
    iron = true,
    water = true,
    hq = true,
    tank = true,
    border = true
    }
local filter = function(other)
    return colltag[other.tag]
end
local filter_no_coll = function (other)
    return other.tag == "tank"
end

function mode:newTank(teams,types,pos,checkColl)
    local x,y
    local ix,iy
    while true do
        ix = math.random(pos.minX,pos.maxX)
        iy = math.random(pos.minY,pos.maxY)
        x = ix*16
        y = iy*16
        local colls,collCount = self.world:queryRect(x-16,y-16,32,32,
            checkColl and filter or filter_no_coll
        )
        if collCount == 0 then
            break
        end
    end
    if not checkColl then
        self.map:setTile(ix+1,iy,6)
        self.map:setTile(ix,iy,6)
        self.map:setTile(ix,iy+1,6)
        self.map:setTile(ix+1,iy+1,6)
    end
    
    local tank = Tank(
            self,
			x, --x
			y,  --y
			math.random(0,3), --rot
			2, --scale
			type(teams) == "number" and teams or teams[math.random(1,#teams)], --team
			type(types) == "number" and types or types[math.random(1,#types)]--type
        )
    return tank
end


function mode:onKill(killer,victim)
    if victim == self.player then
         delay:new(self.playerRebirthCD,
                function() self:setPlayerTank(victim.player) end)
    end
end


return mode
